<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getOverviewApi } from '@/api/home'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0
})

// 快捷操作卡片数据
const quickCards = ref([
  {
    title: '课程管理',
    subtitle: '课程信息维护',
    route: '/resource/course',
    icon: '📚'
  },
  {
    title: '活动管理',
    subtitle: '营销活动管理',
    route: '/resource/activity',
    icon: '🎯'
  },
  {
    title: '线索管理',
    subtitle: '潜在客户追踪',
    route: '/clue/list',
    icon: '🔍'
  },
  {
    title: '商机管理',
    subtitle: '销售机会管理',
    route: '/business/list',
    icon: '💼'
  },
  {
    title: '客户管理',
    subtitle: '客户信息维护',
    route: '/customer',
    icon: '👥'
  },
  {
    title: '日志统计',
    subtitle: '系统操作日志',
    route: '/system/log',
    icon: '📊'
  }
])

// 获取概览数据
const loadOverviewData = async () => {
  try {
    const result = await getOverviewApi()
    if (result.code == 1) {
      overviewData.value = result.data
    } else {
      ElMessage.error(result.msg || '获取概览数据失败')
    }
  } catch (error) {
    console.error('获取概览数据失败:', error)
    ElMessage.error('获取概览数据失败，请重试')
  }
}

// 跳转到对应页面
const navigateTo = (route) => {
  router.push(route)
}

onMounted(() => {
  loadOverviewData()
})
</script>

<template>
  <div class="home-container">
    <!-- 欢迎标题 -->
    <div class="welcome-header">
      <h1>欢迎您!进入轻客管家系统</h1>
    </div>

    <!-- 概览卡片区域 -->
    <div class="overview-section">
      <!-- 线索概览 -->
      <div class="overview-card">
        <h2 class="card-title">线索概览</h2>
        <div class="overview-grid">
          <div class="overview-item">
            <div class="item-label">总线索数</div>
            <div class="item-value">{{ overviewData.clueTotal }}</div>
          </div>
          <div class="overview-item">
            <div class="item-label">待分配线索数</div>
            <div class="item-value">{{ overviewData.clueWaitAllot }}</div>
          </div>
          <div class="overview-item">
            <div class="item-label">待跟进线索数</div>
            <div class="item-value">{{ overviewData.clueWaitFollow }}</div>
          </div>
          <div class="overview-item">
            <div class="item-label">跟进中线索数</div>
            <div class="item-value">{{ overviewData.clueFollowing }}</div>
          </div>
          <div class="overview-item">
            <div class="item-label">伪线索数</div>
            <div class="item-value">{{ overviewData.clueFalse }}</div>
          </div>
          <div class="overview-item">
            <div class="item-label">转商机线索数</div>
            <div class="item-value">{{ overviewData.clueConvertBusiness }}</div>
          </div>
        </div>
      </div>

      <!-- 商机概览 -->
      <div class="overview-card">
        <h2 class="card-title">商机概览</h2>
        <div class="overview-grid">
          <div class="overview-item">
            <div class="item-label">总商机数</div>
            <div class="item-value">{{ overviewData.businessTotal }}</div>
          </div>
          <div class="overview-item">
            <div class="item-label">待分配商机数</div>
            <div class="item-value">{{ overviewData.businessWaitAllot }}</div>
          </div>
          <div class="overview-item">
            <div class="item-label">待跟进商机数</div>
            <div class="item-value">{{ overviewData.businessWaitFollow }}</div>
          </div>
          <div class="overview-item">
            <div class="item-label">跟进中商机数</div>
            <div class="item-value">{{ overviewData.businessFollowing }}</div>
          </div>
          <div class="overview-item">
            <div class="item-label">回收商机数</div>
            <div class="item-value">{{ overviewData.businessFalse }}</div>
          </div>
          <div class="overview-item">
            <div class="item-label">转客户商机数</div>
            <div class="item-value">{{ overviewData.businessConvertCustomer }}</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-access-section">
      <div 
        v-for="card in quickCards" 
        :key="card.title"
        class="quick-card"
        @click="navigateTo(card.route)"
      >
        <div class="card-icon">{{ card.icon }}</div>
        <div class="card-content">
          <h3 class="card-title">{{ card.title }}</h3>
          <p class="card-subtitle">{{ card.subtitle }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100%;
}

.welcome-header {
  text-align: center;
  margin-bottom: 30px;
}

.welcome-header h1 {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.overview-section {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
}

.overview-card {
  flex: 1;
  background-color: #fff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.card-title {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  margin: 0 0 20px 0;
  text-align: center;
}

.overview-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.overview-item {
  border: 1px solid #e4e7ed;
  border-radius: 6px;
  padding: 16px;
  text-align: center;
  background-color: #fafafa;
}

.item-label {
  font-size: 12px;
  color: #909399;
  margin-bottom: 8px;
}

.item-value {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.quick-access-section {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 20px;
}

.quick-card {
  background-color: #fff;
  border-radius: 8px;
  padding: 24px;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid transparent;
}

.quick-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  border-color: #409eff;
}

.card-icon {
  font-size: 32px;
  margin-bottom: 16px;
}

.card-content {
  text-align: center;
}

.card-content .card-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin: 0 0 8px 0;
}

.card-subtitle {
  font-size: 12px;
  color: #909399;
  margin: 0;
  line-height: 1.4;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .quick-access-section {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .overview-section {
    flex-direction: column;
  }
  
  .quick-access-section {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .overview-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
